<template>
  <div class="w">
    <div class="top">
      <span>
        <i class="iconfont icon-xiangzuojiantou" @click="$router.push('/employee')"></i>
      </span>
      <p>添加员工</p>
    </div>
    <div class="con">
      <van-form @submit="onSubmit" ref="submit">
      <van-field
        v-model="lxName"
        name="性名"
        label="性名"
        placeholder="请填入性名"
        autocomplete="off"
        :rules="[{ required: true, message: '请填入性名' }]"
        />
      <van-field name="sex" label="性别">
          <template #input>
            <van-radio-group v-model="sex" direction="horizontal">
              <van-radio name="0" shape="square" checked-color="#ff4c4c">男</van-radio>
              <van-radio name="1" shape="square" checked-color="#ff4c4c">女</van-radio>
            </van-radio-group>
          </template>
      </van-field>
      <van-field
        v-model="lxPhone"
        name="手机号"
        label="手机号"
        placeholder="手机号"
        autocomplete="off"
        :rules="[{ required: true,
           message: '请填正确格式的手机号',
           pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
           }]"
       />
       <van-field
        v-model="pwd"
        type="password"
        name="设置初始密码"
        label="设置初始密码"
        placeholder="设置初始密码"
        :rules="[{
         required: true,
         message: '密码必须由数字、字母两种字符组成，长度在8-15位之间',
         pattern:/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,15}$/}]"
        autocomplete="new-password"
      />
      <van-field
        v-model="password"
        type="password"
        name="确认初始密码"
        label="确认初始密码"
        placeholder="确认初始密码"
        :rules="[{ validator , message: '密码不一致' }]"
        autocomplete="new-password"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" color="#ff4c4c">提交</van-button>
      </div>
    </van-form>
  </div>
  </div>
</template>

<script>
import { Add } from '@/api/information'
import { Toast } from 'vant'
export default {
  data () {
    return {
      // 性名
      lxName: '',
      // 性别
      sex: '0',
      lxPhone: '',
      password: '',
      pwd: '',
      companyId: ''
    }
  },
  methods: {
    // 两次密码校验
    validator (val) {
      return this.pwd === val
    },
    async onSubmit () {
      const res = {
        account: this.lxPhone,
        pwd: this.pwd,
        sex: this.sex,
        lxPhone: this.lxPhone,
        lxName: this.lxName,
        companyId: this.companyId
      }
      const data = await Add(res)
      // console.log(data)
      if (data.success) {
        Toast(data.info)
        // 清空表单
        Object.assign(this.$data, this.$options.data())
      } else {
        Toast(data.info + '添加失败')
      }
    }
  },
  mounted () {
    this.companyId = this.$route.query.companyId
  }
}
</script>

<style lang="less" scoped>
.w{
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  .top{
    width: 100%;
    height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
    span{
      position: absolute;
      left: 20px;
    }
    p{
      text-align: center;
      font-size: 36px;
    }
  }
  .con{
    flex-grow: 1;
    >ul{
      list-style: none;
      >li{
        box-sizing: border-box;
        padding: 0 20px;
        border-radius: 10px;
        width: 680px;
        height: 100px;
        background-color: #f7f7f7;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        >span{
          font-size: 26px;
          color: #666666;
        }
        >span:last-child{
          color: #999999;
        }
      }
    }
  }
  .btn{
    width: 100%;
    height: 200px;
    display: flex;
    justify-content: center;
    align-items: center;
    >div{
      background-color: #ff4c4c;
      color: #ffffff;
      font-size: 30px;
      width: 680px;
      height: 80px;
      border-radius: 10px;
      line-height: 80px;
      text-align: center;
    }
  }
}
</style>
